package org.wu.framework.authorization.platform.infrastructure.persistence;


import jakarta.annotation.Resource;
import org.springframework.stereotype.Repository;
import org.wu.framework.authorization.platform.infrastructure.converter.SysRoleMenuConverter;
import org.wu.framework.authorization.platform.infrastructure.entity.SysMenuDO;
import org.wu.framework.authorization.platform.infrastructure.entity.SysRoleMenuDO;
import org.wu.framework.authorization.platform.domain.model.sys.menu.SysMenu;
import org.wu.framework.authorization.platform.domain.model.sys.role.menu.SysRoleMenu;
import org.wu.framework.authorization.platform.domain.model.sys.role.menu.SysRoleMenuRepository;
import org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyInfrastructurePersistence;
import org.wu.framework.lazy.orm.database.lambda.stream.lambda.LazyLambdaStream;
import org.wu.framework.lazy.orm.database.lambda.stream.wrapper.LazyWrappers;
import org.wu.framework.lazy.orm.web.plus.AbstractLazyCrudRepository;
import org.wu.framework.web.response.Result;
import org.wu.framework.web.response.ResultFactory;

import java.util.List;
import java.util.stream.Collectors;

/**
 * describe 角色菜单
 *
 * @author Jia wei Wu
 * @date 2023/05/17 11:19 晚上
 * @see DefaultDDDLazyInfrastructurePersistence
 **/
@Repository
public class SysRoleMenuRepositoryImpl extends AbstractLazyCrudRepository<SysRoleMenuDO, SysRoleMenu, Integer> implements SysRoleMenuRepository {

    @Resource
    LazyLambdaStream lazyLambdaStream;


    /**
     * describe 新增角色菜单
     *
     * @param
     * @return
     * @exception/throws
     * @author Jia wei Wu
     * @date 2023/05/17 11:19 晚上
     **/

    @Override
    public Result<SysRoleMenu> story(SysRoleMenu sysRoleMenu) {
        SysRoleMenuDO sysRoleMenuDO = SysRoleMenuConverter.fromRoleMenu(sysRoleMenu);
        lazyLambdaStream.upsert(sysRoleMenuDO);
        return ResultFactory.successOf();
    }

    /**
     * describe 新增角色菜单
     *
     * @param sysRoleMenuList@return
     * @exception/throws
     * @author Jia wei Wu
     * @date 2023/05/17 11:19 晚上
     **/
    @Override
    public Result batchStory(List<SysRoleMenu> sysRoleMenuList) {
        List<SysRoleMenuDO> sysRoleMenuDOList = sysRoleMenuList.stream().map(SysRoleMenuConverter::fromRoleMenu).collect(Collectors.toList());
        lazyLambdaStream.upsert(sysRoleMenuDOList);
        return ResultFactory.successOf();
    }

    /**
     * describe 查询单个角色菜单
     *
     * @param
     * @return
     * @exception/throws
     * @author Jia wei Wu
     * @date 2023/05/17 11:19 晚上
     **/

    @Override
    public Result<SysRoleMenu> findOne(SysRoleMenu sysRoleMenu) {
        SysRoleMenuDO sysRoleMenuDO = SysRoleMenuConverter.fromRoleMenu(sysRoleMenu);
        SysRoleMenu sysRoleMenuOne = lazyLambdaStream.selectOne(LazyWrappers.lambdaWrapperBean(sysRoleMenuDO), SysRoleMenu.class);
        return ResultFactory.successOf(sysRoleMenuOne);
    }

    /**
     * describe 查询多个角色菜单
     *
     * @param
     * @return
     * @exception/throws
     * @author Jia wei Wu
     * @date 2023/05/17 11:19 晚上
     **/

    @Override
    public Result<List<SysRoleMenu>> findList(SysRoleMenu sysRoleMenu) {
        SysRoleMenuDO sysRoleMenuDO = SysRoleMenuConverter.fromRoleMenu(sysRoleMenu);
        List<SysRoleMenu> sysRoleMenuList = lazyLambdaStream.selectList(LazyWrappers.lambdaWrapperBean(sysRoleMenuDO), SysRoleMenu.class);
        return ResultFactory.successOf(sysRoleMenuList);
    }

    /**
     * 获取菜单数据
     *
     * @param module 木块
     * @param roleIds 角色ID
     * @return Result<List<SysMenu>>
     */
    @Override
    public Result<List<SysMenu>> findRoleMenuList(String module, List<Long> roleIds) {
        List<SysMenu> sysMenuList = lazyLambdaStream.selectList(LazyWrappers.<SysRoleMenuDO>lambdaWrapper()
                .in(SysRoleMenuDO::getRoleId, roleIds)
//                        .distinct(SysRoleMenuDO::getMenuId)
                .internalJoin(LazyWrappers.<SysRoleMenuDO, SysMenuDO>lambdaWrapperJoin()
                        .eq(SysRoleMenuDO::getMenuId, SysMenuDO::getId)
                        .eqo(SysRoleMenuDO::getIsDeleted,false)
                        .eqRighto(SysMenuDO::getIsDeleted,false)
                        .eqRighto(SysMenuDO::getModule,module)
                )
//                        .groupBy(SysRoleMenuDO::getMenuId)

                , SysMenu.class
        );
        return ResultFactory.successOf(sysMenuList);
    }

    /**
     * describe 删除角色菜单
     *
     * @param
     * @return
     * @exception/throws
     * @author Jia wei Wu
     * @date 2023/05/17 11:19 晚上
     **/

    @Override
    public Result<SysRoleMenu> remove(SysRoleMenu sysRoleMenu) {
        SysRoleMenuDO sysRoleMenuDO = SysRoleMenuConverter.fromRoleMenu(sysRoleMenu);
        sysRoleMenuDO.setIsDeleted(true);
        lazyLambdaStream.upsert(sysRoleMenuDO);
        return ResultFactory.successOf();
    }

    /**
     * describe 删除角色菜单
     *
     * @param roleId
     * @exception/throws
     * @author Jia wei Wu
     * @date 2023/05/17 11:19 晚上
     **/
    @Override
    public void removeRoleMenus(Long roleId) {
        SysRoleMenuDO sysRoleMenuDO = new SysRoleMenuDO();
        sysRoleMenuDO.setIsDeleted(true);
        lazyLambdaStream.update(sysRoleMenuDO, LazyWrappers.<SysRoleMenuDO>lambdaWrapper()
                .eq(SysRoleMenuDO::getRoleId, roleId)
        );
    }
}